package cn.kongwg.demo.controller;

import java.io.*;
import java.net.URLEncoder;
import java.sql.Timestamp;

import cn.kongwg.demo.entry.LayuiEntry;
import cn.kongwg.demo.entry.TabFile;
import cn.kongwg.demo.service.TabFileService;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author kongwg
 * @create 2021-05-24 17:20
 */
@RestController
public class UploadController {

    @Autowired
    private TabFileService tabFileService;

    public static final String PATH = "F:\\upload";

    @RequestMapping("/upload")
    public LayuiEntry upload(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        // FileOutputStream fos = new FileOutputStream(new File(PATH + originalFilename));
        String fileUrl = PATH + "/" + originalFilename;
        file.transferTo(new File(fileUrl));
        TabFile tabFile = new TabFile();
        tabFile.setFilename(originalFilename);
        tabFile.setFileurl(fileUrl);
        tabFile.setCreatetime(new Timestamp(new java.util.Date().getTime()));
        tabFile.setUpperson("pipi");
        tabFileService.save(tabFile);
        return new LayuiEntry(0, null, 0, null);
    }

    @RequestMapping("/dow")
    public LayuiEntry dow(HttpServletResponse response,
                          @RequestParam("fileid") long fileid) {
        TabFile tabFile = tabFileService.getById(fileid);
        String fileurl = tabFile.getFileurl();

        FileInputStream fis = null;
        ServletOutputStream sos = null;
        try {
            //下载文件的名字
            response.reset();//清除缓存
            response.setCharacterEncoding("utf-8");//设置字符编码
            response.setContentType("multipart/form-data");//下载方式使用二进制

            //下载文件的名字使用中文时不出现乱码
            response.setHeader("Content-Disposition", "attachment;fileName=" +
                    URLEncoder.encode(tabFile.getFilename(), "UTF-8"));

            //输入流
            fis = new FileInputStream(fileurl);

            //输出流
            sos = response.getOutputStream();

            //将输入流拷贝进输出流中
            IOUtils.copy(fis, sos);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭流
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (sos != null) {
                try {
                    sos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

        return new LayuiEntry();
    }

    @RequestMapping("/list")
    public LayuiEntry show(@PathVariable(required = false) Integer page, @PathVariable(required = false) Integer limit) {
        List<Map<String, Object>> map = tabFileService.listMaps();
        Map<String, Object> map1 = map.get(0);
        System.out.println("map = " + map1);
        return new LayuiEntry(0, "ok", 0, map);
    }
       @RequestMapping("/del")
    public LayuiEntry delete(@RequestParam("fileid") long fileid) {
           boolean b = tabFileService.removeById(fileid);
           return new LayuiEntry(0, "ok", 0, b);
    }


}
